package com.example.controller;

import com.example.entity.Users;
import com.example.service.UsersService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2022-06-08 23:32:13
 */
@Api("会员信息")
@Controller
@RequestMapping("/admin")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersService usersService;

    /**
     * 分页查询
     *

     * @return 查询结果
     */


    @GetMapping("/getUserById")
    public Users selectOne(Integer id) {
        return this.usersService.queryById(id);
    }

    @GetMapping("/users")
    public String queryAll(Users users,
                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                           Model model){
        PageInfo<Users> usersListPage = usersService.queryAllByPage(users,pageNum,pageSize);
        model.addAttribute("usersListPage",usersListPage);
        return "userslist";
    }
    @PostMapping("/addUser")
    public Users addUser(Users users){
        return  this.usersService.insert(users);

    }

    @PostMapping("/editUser")
    public Users editUser(Users users){
        return  this.usersService.update(users);
    }

    @GetMapping("/delUserById/{uid}")
    public String deleteById(@PathVariable("uid") Integer uid){
        this.usersService.deleteById(uid);
        return "forward:/admin/users";
    }

}

